package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.fault.FaultFeedbackManager;
import com.sduept.nwld.dataserver.model.daily.FaultFeedback;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class FaultFeedbackController extends DailyReportAbstractController {

	private static final long serialVersionUID = 1739959360308869436L;
	@Autowired
	private FaultFeedbackManager ffm;
	private List<FaultFeedback> faultFeedbacks = new ArrayList<>();
	private FaultFeedback created;
	private FaultFeedback selected;

	@PostConstruct
	public void init() {
		faultFeedbacks = ffm.findFaultFeedback(queryDate1, queryDate2);
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
	}

	public void queryByTime() {
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		queryDate2 = DateUtil.addDays(queryDate2, 1);
		queryDate2 = DateUtil.addSeconds(queryDate2, -1);
		faultFeedbacks = ffm.findFaultFeedback(queryDate1, queryDate2);
		refreshQueryParamter();
		selected = null;
		created = null;
	}

	public void preCreate() {
		created = new FaultFeedback();
		created.setFaultTime(new Date());
	}

	public void preEdit() {
		if (selected != null) {
			created = selected;
		}
	}

	public void save() {
		if (created.getId() == null) {
			created.setId(ObjectId.get().toString());
		}
		ffm.save(created);
		queryByTime();
		created = null;
	}

	public void delete() {
		if (selected != null) {
			ffm.delete(selected);
		}
		queryByTime();
		selected = null;
	}

	public List<FaultFeedback> getFaultFeedbacks() {
		return faultFeedbacks;
	}

	public void setFaultFeedbacks(List<FaultFeedback> faultFeedbacks) {
		this.faultFeedbacks = faultFeedbacks;
	}

	public FaultFeedback getCreated() {
		return created;
	}

	public void setCreated(FaultFeedback created) {
		this.created = created;
	}

	public FaultFeedback getSelected() {
		return selected;
	}

	public void setSelected(FaultFeedback selected) {
		this.selected = selected;
	}

}
